{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"id":"VzKzjJJb542a"},"outputs":[],"source":["%pip install sentence_transformers\n","\n","import numpy as np\n","import pandas as pd\n","from sentence_transformers import SentenceTransformer, util"]},{"cell_type":"code","execution_count":88,"metadata":{"executionInfo":{"elapsed":4596,"status":"ok","timestamp":1685103467723,"user":{"displayName":"Marcel Garz","userId":"11492880614359963597"},"user_tz":-120},"id":"Oowd_7-Snsxc"},"outputs":[],"source":["model = SentenceTransformer('sentence-transformers/paraphrase-multilingual-mpnet-base-v2')"]},{"cell_type":"markdown","metadata":{},"source":["All excerpts used in the notebook were manually collected from https://visuals.manifesto-project.wzb.eu/mpdb-shiny/cmp_dashboard_dataset/"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":453,"status":"ok","timestamp":1685097135609,"user":{"displayName":"Marcel Garz","userId":"11492880614359963597"},"user_tz":-120},"id":"zbJOxsR4YVnj","outputId":"c3c67a40-dcde-4e15-c637-f82af7ba1c47"},"outputs":[{"data":{"text/plain":["0.56211835"]},"execution_count":68,"metadata":{},"output_type":"execute_result"}],"source":["# Table 1, example 1\n","\n","# Democratic Party US, 2004\n","p1 = [\"Now, as never before, education is the key to opportunity, essential to a strong America.\",\n","       \"So we believe in an America that offers the best education to all our children - wherever they live, whatever their background. Period\",\n","       \"We believe in an America where every child comes to school ready to learn.\"]\n","\n","# Republican Party US, 2004\n","p2 = [\"Education: No Child Left Behind\",\n","       \"Public education, access for every child to an excellent education, is a foundation of a free, civil society.\",\n","       \"The children who enter schools today will leave as young adults, full of dreams for the future.\"]\n","\n","p1 = model.encode(p1, convert_to_tensor=True)\n","p2 = model.encode(p2, convert_to_tensor=True)\n","\n","cosine_scores = util.cos_sim(p1, p2)\n","cosine_scores = pd.DataFrame(cosine_scores.numpy())\n","cosine_scores['rowmean'] = cosine_scores.mean(axis=1)\n","cosine_scores[\"rowmean\"].mean()"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":2082,"status":"ok","timestamp":1685092944814,"user":{"displayName":"Marcel Garz","userId":"11492880614359963597"},"user_tz":-120},"id":"DhDxmaOVVaKu","outputId":"ca5250fd-462d-4be1-d147-ffcdaa1a6786"},"outputs":[{"data":{"text/plain":["0.6244172"]},"execution_count":37,"metadata":{},"output_type":"execute_result"}],"source":["# Table 1, example 2\n","\n","# New Zealand National Party, 2017\n","p1 = [\"Māori businesses are increasingly influential and internationally connected and they are an important part of New Zealand’s story and economic success.\",\n","       \"Te Reo Māori and Māori culture are significant assets that promote the unique identity and place of Māori in New Zealand and our presence in the global community.\",\n","       \"We are working with Māori to address social challenges and making significant progress on Treaty settlements.\"]\n","\n","# New Zealand Labour Party, 2017\n","p2 = [\"Supporting Te Reo Māori in schools\",\n","       \"The survival and future of Te Reo Māori as a thriving language is a core value for Labour because we believe it is a unique taonga for New Zealanders.\",\n","       \"It is also fundamental to our national identity, and it provides an important gateway to better understanding the rich history and culture of Aotearoa.\",\n","       \"The Māori language forms part of the broader cultural identity and heritage of New Zealand.\"]\n","       \n","p1 = model.encode(p1, convert_to_tensor=True)\n","p2 = model.encode(p2, convert_to_tensor=True)\n","\n","cosine_scores = util.cos_sim(p1, p2)\n","cosine_scores = pd.DataFrame(cosine_scores.numpy())\n","cosine_scores['rowmean'] = cosine_scores.mean(axis=1)\n","cosine_scores[\"rowmean\"].mean()"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":6,"status":"ok","timestamp":1685097716802,"user":{"displayName":"Marcel Garz","userId":"11492880614359963597"},"user_tz":-120},"id":"YS7RwLLKg5dR","outputId":"02b0e3d8-d02c-443a-dc51-9737db87a22b"},"outputs":[{"data":{"text/plain":["0.24701144"]},"execution_count":72,"metadata":{},"output_type":"execute_result"}],"source":["# Table 2, example 1\n","\n","# Labour Party UK, 2015\n","p1 = ['We will reform the Work Capability Assessment and focus it on the support disabled people need to get into work.',\n","       'We will give an independent scrutiny group of disabled people a central role in monitoring it.',\n","       'And we will introduce a specialist support programme to ensure that disabled people who can work get more tailored help.']\n","\n","# Conservative Party UK, 2015\n","p2 = [\"We will continue to raise the quality of children’s social work, by expanding training programmes, such as Frontline, and creating new opportunities to develop the next generation of leaders in the field.\",\n","       \"We will continue to tackle all forms of bullying in our schools.\"]\n","       \n","p1 = model.encode(p1, convert_to_tensor=True)\n","p2 = model.encode(p2, convert_to_tensor=True)\n","\n","cosine_scores = util.cos_sim(p1, p2)\n","cosine_scores = pd.DataFrame(cosine_scores.numpy())\n","cosine_scores['rowmean'] = cosine_scores.mean(axis=1)\n","cosine_scores[\"rowmean\"].mean()"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":9,"status":"ok","timestamp":1685098716296,"user":{"displayName":"Marcel Garz","userId":"11492880614359963597"},"user_tz":-120},"id":"EdwgVcYhsFrQ","outputId":"b38983aa-ec42-4182-c63e-692d9a578237"},"outputs":[{"data":{"text/plain":["0.22418487"]},"execution_count":77,"metadata":{},"output_type":"execute_result"}],"source":["# Table 2, example 2\n","\n","# Democratic Party US, 2012\n","p1 = [\"Immigration. Democrats are strongly committed to enacting comprehensive immigration reform that supports our economic goals and reflects our values as both a nation of laws and a nation of immigrants.\",\n","       \"The story of the United States would not be possible without the generations of immigrants who have strengthened our country and contributed to our economy.\"]\n","\n","# Republican Party US, 2012\n","p2 = [\"Honoring Our Relationship with American Indians\",\n","       \"Based on both treaty and other law, the federal government has a unique government-to-government relationship with and trust responsibility for Indian Tribal Governments and American Indians and Alaska Natives.\",\n","       \"These obligations have not been sufficiently honored.\"]\n","       \n","p1 = model.encode(p1, convert_to_tensor=True)\n","p2 = model.encode(p2, convert_to_tensor=True)\n","\n","cosine_scores = util.cos_sim(p1, p2)\n","cosine_scores = pd.DataFrame(cosine_scores.numpy())\n","cosine_scores['rowmean'] = cosine_scores.mean(axis=1)\n","cosine_scores[\"rowmean\"].mean()"]}],"metadata":{"colab":{"authorship_tag":"ABX9TyOpATB2PjUC3TwY83g+F7bq","gpuType":"T4","machine_shape":"hm","provenance":[{"file_id":"1JiETrAuWo-HA1AiXlHgn0Xoh7920jOh1","timestamp":1685026673922},{"file_id":"1xk0dzNVtn37pnn0R9r-TGNWonW1eS-C5","timestamp":1684831223377},{"file_id":"1hHYukMSws777vWhImJd6l80n_3pE_BqX","timestamp":1684485132137},{"file_id":"1jX3khvM5Q9M7xd-c50yefPwJS-BI3xrK","timestamp":1684390167556},{"file_id":"13f9ueU8KdeidCbTJ5RgegLeGGOIdyKTY","timestamp":1684302769764},{"file_id":"11l0nN6YqzWJHN2KQHPsztztYbl8eyAdz","timestamp":1684161524757},{"file_id":"16Xy1RY4oVz4HN6ofuOLLrKvAOBNVkC17","timestamp":1683614188215}]},"gpuClass":"standard","kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.11.9"}},"nbformat":4,"nbformat_minor":0}
